package com.wkf.truck.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkf.truck.common.R;
import com.wkf.truck.model.Address;
import com.wkf.truck.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lxb
 * @since 2021-02-14
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 保存
     *
     * @param address
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Address address) {
        addressService.saveOrUpdate(address);
        return R.ok();
    }

    @GetMapping("/getAddressDetails")
    public R getDetails(@RequestParam("userId") Long userId, @RequestParam("addressId") Long addressId) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("id", addressId);
        Address one = addressService.getOne(wrapper);

        return R.ok().put("data", one);
    }

    /**
     * 删除
     * @param addressId
     * @return
     */
    @GetMapping("/deleteAddress")
    public R delete(@RequestParam("addressId") Long addressId){
        addressService.removeById(addressId);
        return R.ok();

    }


}
